﻿using System;
using System.Collections.Generic;
using System.Net;
using System.IO;
using Utilities.FTP;
using Lidgren.Network;
using Microsoft.Xna.Framework;

namespace HighFantasyRPG
{
   public class Network
    {

        // Client Object
        static NetClient Client;
        public static bool SessionStarted;
        public static string message, debug;

        public enum PacketTypes
        {
            LOGIN,
            MOVE,
            WORLDSTATE
        }

       public static bool CreateSession ()
        {

            

            

            // Create new instance of configs. Parameter is "application Id". It has to be same on client and server.
            NetPeerConfiguration Config = new NetPeerConfiguration("HF");
            // Enable DiscoveryResponse messages
            Config.EnableMessageType(NetIncomingMessageType.DiscoveryResponse);

            // Create new client, with previously created configs
            Client = new NetClient(Config);

            // Emit a discovery signal
            Client.DiscoverLocalPeers(14242);

            // Create new outgoing message
            NetOutgoingMessage outmsg = Client.CreateMessage();


            //LoginPacket lp = new LoginPacket("Katu");

            // Start client
            Client.Start();

            // Write byte ( first byte informs server about the message type ) ( This way we know, what kind of variables to read )
            outmsg.Write((byte)PacketTypes.LOGIN);

            // Write String "Name" . Not used, but just showing how to do it
            outmsg.Write("Player");

            

            
            
            // Connect client, to ip previously requested from user 
            if (Client.Connect("192.168.1.50", 14242, outmsg) != null)
               {
            message = "Created player";
            debug = Client.Configuration.LocalAddress.ToString();
            SessionStarted = true;
            return true;
               }

           message = "Cannot connect";
           return false;
        }

       // Before main looping starts, we loop here and wait for approval message
       public static void WaitForStartingInfo()
       {
           if (SessionStarted == true)
           {
           // New incomgin message
           NetIncomingMessage inc;

           

               // If new messages arrived
               if ((inc = Client.ReadMessage()) != null)
               {
                   // Switch based on the message types
                   switch (inc.MessageType)
                   {

                       // All manually sent messages are type of "Data"
                       case NetIncomingMessageType.Data:

                           // Read the first byte
                           // This way we can separate packets from each others
                           if (inc.ReadByte() == (byte)PacketTypes.WORLDSTATE)
                           {
                              


                               // When all players are added to list, start the game
                               
                           }
                           break;

                       default:
                           // Should not happen and if happens, don't care
                           
                           break;
                   }
               }
           }
       }

       public static void CheckServerMessages()
       {
           // Create new incoming message holder
           NetIncomingMessage inc;

          

           while ((inc = Client.ReadMessage()) != null)
           {
               if (inc.MessageType == NetIncomingMessageType.Data)
               {
                   if (inc.ReadByte() == (byte)PacketTypes.WORLDSTATE)
                   {
                       message = "Sending data";
                       
                   }
               }
               else if (inc.MessageType == NetIncomingMessageType.DiscoveryResponse)
 
            message = "Found server at " + inc.SenderEndpoint + " name: " + inc.ReadString();
            break;
           }
       }

       public static void SendPlayerData(int direction, Vector2 pos)
       {

           if (SessionStarted == true)

               {
                   // Create new message
                   NetOutgoingMessage outmsg = Client.CreateMessage();

                   // Write byte = Set "MOVE" as packet type
                   outmsg.Write((byte)PacketTypes.MOVE);

                   // Write byte = move direction
                   outmsg.Write((byte)direction);
                   outmsg.Write((float)pos.X);
                   outmsg.Write((float)pos.Y);

                   // Send it to server
                   Client.SendMessage(outmsg, NetDeliveryMethod.ReliableOrdered);

               }

       }

       public static void UploadFile (string file)
         {
             FTPclient ftp = new FTPclient("ftp://strategywars.bugs3.com", "u495661866", "casa514");
             ftp.CurrentDirectory = "/public_html/";
             ftp.Upload(AppDomain.CurrentDomain.BaseDirectory + "/Content/Maps/" + file, "rpg/maps/" + file);


         }

       public static void Download (string file)

           {
               FTPclient ftp = new FTPclient("ftp://strategywars.bugs3.com", "u495661866", "casa514");
               ftp.CurrentDirectory = "/public_html/";
               ftp.Download("rpg/maps/" + file, AppDomain.CurrentDomain.BaseDirectory + "/Content/Maps/" + file, true);
           }
    }
}
